package com.mifengs.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mifengs.mall.R;
import com.mifengs.mall.entity.GoodsBean;
import com.mifengs.mall.entity.ScareBuyingBean;
import com.mifengs.mall.entity.UpdateData;
import com.mifengs.mall.ui.web.CommonWebActivity;
import com.mifengs.mall.util.ImageLoadUtil;
import com.mifengs.mall.util.ToIntentUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lihaoqi on 2017/11/3.
 */

public class LoopGoodsAdapter extends  RecyclerView.Adapter<LoopGoodsAdapter.VH> {


    private Context mContext;

    private List<GoodsBean> goodsBeanList = new ArrayList<>();

    private MyListener mListener;


    public LoopGoodsAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setGoodsBeanList(List<GoodsBean> goodsBeanList) {
        this.goodsBeanList = goodsBeanList;
        notifyDataSetChanged();
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        return new VH(LayoutInflater.from(mContext).inflate(R.layout.item_loop_goods,parent,false));
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
            int size = goodsBeanList.size();
            if(size!=0){
                int index = position%size;
                GoodsBean bean = goodsBeanList.get(index);
                ImageLoadUtil.displayImage(mContext,bean.getImg(),holder.ivImg);
                double discount = Double.parseDouble(bean.getDiscount())*10;
                holder.tvDiscount.setText(discount+"折");
                holder.tvName.setText(bean.getGoodName());
                holder.tvXianjia.setText("¥"+bean.getDiscountPrice());
                holder.tvYuanjia.setText("¥"+bean.getOriginalCost());
                holder.tvYuanjia.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                holder.tvYuanjia.getPaint().setAntiAlias(true);
                //mListener = new MyListener(bean);
                holder.itemView.setOnClickListener(new MyListener(bean));
            }
    }

    @Override
    public int getItemCount() {
        if(goodsBeanList.size()==0){
            return 0;
        }else{
            return Integer.MAX_VALUE;
        }
    }

    class VH extends RecyclerView.ViewHolder {

        private ImageView ivImg;
        private TextView tvName;
        private TextView tvDiscount;
        private TextView tvXianjia;
        private TextView tvYuanjia;

        public VH(View itemView) {
            super(itemView);
            ivImg = itemView.findViewById(R.id.iv_img);
            tvName = itemView.findViewById(R.id.tv_name);
            tvDiscount = itemView.findViewById(R.id.tv_discount);
            tvXianjia = itemView.findViewById(R.id.tv_xianjia);
            tvYuanjia = itemView.findViewById(R.id.tv_yuanjia);
        }
    }


    class MyListener implements View.OnClickListener{


        private GoodsBean bean;

        public MyListener(GoodsBean bean) {
            this.bean = bean;
        }

        @Override
        public void onClick(View v) {
            ToIntentUtil.toCommonIntent(bean.getActionUrl(),mContext);
        }
    }

}
